<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Home\HomeDashboard;
use App\Admin\Metrics\Posts\AllCircle;
use App\Admin\Metrics\Posts\PostsTickets;
use App\Admin\Metrics\Users\AllUsers;
use App\Admin\Metrics\Users\AuthUsers;
use App\Admin\Metrics\Users\VipUsers;
use App\Http\Controllers\Controller;
use App\Jobs\System\CheckAllTableCharsetJob;
use App\Jobs\System\CheckBatchCahceDataLossJob;
use App\Jobs\System\SettingImgToJpgJob;
use App\Jobs\System\StrReplaceStrJob;
use App\Jobs\System\SubMenuPermissionCheck;
use App\Models\WxUniversity;
use App\Wen\Utils\Settings;

use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        global $__MINI_GLOBAL_TENANT_ID__;

        $this->init_pay_codes();

        if($__MINI_GLOBAL_TENANT_ID__ > 0){
            return $content
                ->header('分站控制台')
                ->description('')
                ->body(function (Row $row) {
                    $row->column(6, function (Column $column) {
                        $column->row(HomeDashboard::title());
                        $column->row(new PostsTickets());
                    });

                    $row->column(6, function (Column $column) {
                        $column->row(function (Row $row) {
                            $row->column(6, new AuthUsers());
                            $row->column(6, new VipUsers());
                        });

                        $column->row(new AllUsers());
                        $column->row(new AllCircle());
                    });
                });
        }else{
            return $content
                ->header('统计控制台')
                ->description('')
                ->body(function (Row $row) {
                    $row->column(6, function (Column $column) {
                        $column->row(HomeDashboard::title());
                        $column->row(new PostsTickets());
                    });

                    $row->column(6, function (Column $column) {
                        $column->row(function (Row $row) {
                            $row->column(6, new AuthUsers());
                            $row->column(6, new VipUsers());
                        });

                        $column->row(new AllUsers());
                        $column->row(new AllCircle());
                    });
                });
        }

    }

    public function init_pay_codes(){
        global $__MINI_GLOBAL_VERSION__;
        if(Settings::get('init_pay_codes:version', '001 000 000 000') < $__MINI_GLOBAL_VERSION__){

            CheckAllTableCharsetJob::dispatch();
            CheckBatchCahceDataLossJob::dispatch();

            if(_empty_(Settings::get('mini_pay_code_1', ''))) {
                Settings::set('mini_pay_code_1', 'https://img.mini.minisns.cn/2023/05/06/3a8150e0c552f7b5649932339da1ca1b.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_2', ''))) {
                Settings::set('mini_pay_code_2', 'https://img.mini.minisns.cn/2023/05/04/ec15503cbea2075128b81c2ab98b64fe.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_3', ''))) {
                Settings::set('mini_pay_code_3', 'https://img.mini.minisns.cn/2023/05/06/271945d1229c94c4f16f850b9b8bb0bf.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_4', ''))) {
                Settings::set('mini_pay_code_4', 'https://img.mini.minisns.cn/2023/05/06/29200f83e9019f1b3c5bbdcd779040b9.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_5', ''))) {
                Settings::set('mini_pay_code_5', 'https://img.mini.minisns.cn/2023/05/06/0c3c850b2b97a9cb035a14bd772e1deb.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_6', ''))) {
                Settings::set('mini_pay_code_6', 'https://img.mini.minisns.cn/2023/05/06/cc056fa268fb069885db46cfde03c00b.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_7', ''))) {
                Settings::set('mini_pay_code_7', 'https://img.mini.minisns.cn/2023/05/06/3b4bf890baf838241e06013d4773a83d.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_8', ''))) {
                Settings::set('mini_pay_code_8', 'https://img.mini.minisns.cn/2023/05/06/2b44259261a4ecdfc014124442061e4e.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_9', ''))) {
                Settings::set('mini_pay_code_9', 'https://img.mini.minisns.cn/2023/05/06/2b3b4497a3197e8552f61e93ae902fee.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_10', ''))) {
                Settings::set('mini_pay_code_10', 'https://img.mini.minisns.cn/2023/05/06/fbea86d62bb267c9a3b501010f1f5c19.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_11', ''))) {
                Settings::set('mini_pay_code_11', 'https://img.mini.minisns.cn/2023/05/06/2c05553eacc02ba730f02889198181e1.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_12', ''))) {
                Settings::set('mini_pay_code_12', 'https://img.mini.minisns.cn/2023/05/06/e32c4861fb43f8fe59008b13f64806ca.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_13', ''))) {
                Settings::set('mini_pay_code_13', 'https://img.mini.minisns.cn/2023/09/04/38292f7ea53932fb984d38756989d434.png', true);
            }
            if(_empty_(Settings::get('mini_pay_code_14', ''))) {
                Settings::set('mini_pay_code_14', 'https://img.mini.minisns.cn/2023/05/05/0074a494fc254d722488fdd41863aa54.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_16', ''))) {
                Settings::set('mini_pay_code_16', 'https://img.mini.minisns.cn/2023/06/02/1c47e36d1d134c18b53accc13250d5a9.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_17', ''))) {
                Settings::set('mini_pay_code_17', 'https://img.mini.minisns.cn/2023/05/06/afdb97216a837dcdd1c8665c3c05ee7f.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_18', ''))) {
                Settings::set('mini_pay_code_18', 'https://img.mini.minisns.cn/2023/05/20/f5103e7535efd5119dfce095fc169c43.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_19', ''))) {
                Settings::set('mini_pay_code_19', 'https://img.mini.minisns.cn/2023/05/20/94c0fc874823266cf0b56c385ad2d9eb.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_20', ''))) {
                Settings::set('mini_pay_code_20', 'https://img.mini.minisns.cn/2023/05/31/48d47b6f319126513410698920bf7eb7.jpg', true);
            }
            if(_empty_(Settings::get('mini_pay_code_21', ''))) {
                Settings::set('mini_pay_code_21', 'https://img.mini.minisns.cn/2023/10/29/60b4cf33768e86b1dc09d79d51ee25cc.png', true);
            }
            if(_empty_(Settings::get('mini_pay_code_22', ''))) {
                Settings::set('mini_pay_code_22', 'https://img.mini.minisns.cn/2023/10/29/7a984bbb2df19ac2f506bf504947eae5.png', true);
            }
            if(_empty_(Settings::get('mini_pay_code_23', ''))) {
                Settings::set('mini_pay_code_23', 'https://img.mini.minisns.cn/2023/08/11/93e6430f6ffca8205bc1c50e2429d3bc.png', true);
            }
            if(_empty_(Settings::get('mini_pay_code_24', ''))) {
                Settings::set('mini_pay_code_24', 'https://img.mini.minisns.cn/2023/08/27/f33f453d12705f1ce41597f1f13112ac.png', true);
            }
            if(_empty_(Settings::get('mini_pay_code_26', ''))) {
                Settings::set('mini_pay_code_26', 'https://img.mini.minisns.cn/2023/09/05/9a59b7cec7b43d18ec96456b5ddace13.png', true);
            }
            if(_empty_(Settings::get('mini_pay_code_27', ''))) {
                Settings::set('mini_pay_code_27', 'https://img.mini.minisns.cn/2023/10/29/8517d17169a01fdb609d1b079408c10d.png', true);
            }

            // todo:
            if(_empty_(Settings::get('mini_pay_code_30', ''))) {
                Settings::set('mini_pay_code_30', 'https://img.mini.minisns.cn/2024/01/22/1c6966ad4a85a7062c4ee476c1623d9d.png', true);
            }

            if(_empty_(Settings::get('mini_pay_code_31', ''))) {
                Settings::set('mini_pay_code_31', 'https://img.mini.minisns.cn/2024/03/02/0/5071fc26ab0e7e31aeef59e35196c663.png', true);
            }

            if(_empty_(Settings::get('mini_pay_code_32', ''))) {
                Settings::set('mini_pay_code_32', 'https://img.mini.minisns.cn/2024/04/05/0/8ece49e0ff75217fe6a33459d48067b2.png', true);
            }

            if(_empty_(Settings::get('mini_pay_code_33', ''))) {
                Settings::set('mini_pay_code_33', 'https://img.mini.minisns.cn/2024/04/21/0/5532bab95b8051dfa9a82043d67fadbd.png', true);
            }

            if(_empty_(Settings::get('mini_pay_code_34', ''))) {
                Settings::set('mini_pay_code_34', 'https://img.mini.minisns.cn/2024/05/28/0/43aab398869588ecfd810a537578c4da.png', true);
            }

            if(_empty_(Settings::get('mini_access_token', '', false, false))){
                Settings::set('mini_access_token', md5(time()));
            }

            $is_official = \App\Models\User\WxUser::where('is_official', 1)->pluck('id')->toArray();
            if($is_official){
                _update_env([
                    'APP_ADMIN_USER_ID' => implode(',', $is_official),
                ], [], 1);
            }else{
                _update_env([
                    'APP_ADMIN_USER_ID' => '',
                ], [], 1);
            }

            SubMenuPermissionCheck::dispatch();

            if(_empty_(Settings::get('app_balance_recharge_list', ''))){
                Settings::set('app_balance_recharge_list', '[{"balance":"1","rmb":"1"},{"balance":"3","rmb":"3"},{"balance":"6","rmb":"6"},{"balance":"12","rmb":"12"},{"balance":"28","rmb":"28"},{"balance":"48","rmb":"48"},{"balance":"78","rmb":"78"},{"balance":"98","rmb":"98"},{"balance":"128","rmb":"128"},{"balance":"168","rmb":"168"},{"balance":"198","rmb":"198"},{"balance":"228","rmb":"228"},{"balance":"300","rmb":"300"}]', true);
            }

            SettingImgToJpgJob::dispatch(['used_mall_index_share_image', 'app_bureau_index_share_image', 'img_share_default']);
            $this->school_data_init();

            Settings::set('init_pay_codes:version', $__MINI_GLOBAL_VERSION__, true);

            if(strpos(Settings::get('user_background_maps', ''), 'img.mini.chongyeapp.com') !== false){
                StrReplaceStrJob::dispatch('img.mini.chongyeapp.com', 'img.mini.minisns.cn');
            }
        }
    }

    private function school_data_init(){
        if(file_exists(storage_path('app/school/2013_school_3072.ini')) && WxUniversity::doesntExist()){
            $content = file_get_contents(storage_path('app/school/2013_school_3072.ini'));
            $content = explode("\n", $content);
            $schools = [];
            if($content){
                $order = 1;
                foreach ($content as $line){
                    if($line){
                        $schol = explode(',', $line);
                        if(_array_key($schol, 1, null)){
                            $schools[] = [
                                'sid' => $schol[1],
                                'name' => $schol[0],
                                'order' => $order,
                                'charge' => _array_key($schol, 2, null),
                                'location' => _array_key($schol, 3, null),
                                'level' => strpos(_array_key($schol, 4, ''), '本科') !== false ? 1 : ( strpos(_array_key($schol, 4, ''), '专科') !== false ? 2 : 3),
                                'private' => strpos(_array_key($schol, 4, ''), '民办') !== false ? 1 : 0,
                                'is_certify' => 0
                            ];
                            $order += 1;
                        }
                    }
                }
            }
            if($schools){
                $chunks = array_chunk($schools, 500); // 将数组分割为每500个一组
                foreach ($chunks as $chunk) {
                    WxUniversity::insert($chunk);
                }
            }
        }
    }

}
